package com.mew.common.dc.db;

import com.mew.common.config.RedisConstantValue;
import com.mew.common.vo.task.GameTaskBO;
import com.mew.common.vo.task.TaskBO;
import com.mew.common.dc.mapper.TaskGameMapper;
import com.mew.common.dc.mapper.TaskLobbyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 任务
 * @author dylin
 * @create 2021/3/29
 */
@Service
@Slf4j
public class TaskDBService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private TaskLobbyMapper taskLobbyMapper;
    @Autowired
    private TaskGameMapper taskGameMapper;
    /**
     * 加载任务库
     */
    public void loadTaskLibrary(){
        log.info("------ load task library -----------");
        loadLobbyTaskLibrary();
        loadGameTaskLibrary();
    }

    /**
     * 加载大厅任务库
     */
    public void loadLobbyTaskLibrary(){
        log.info("------ load lobby task library -----------");
        List<TaskBO> lobbyTaskList = taskLobbyMapper.getLobbyTaskList();
        log.info("------ lobbyTaskList : {}",lobbyTaskList );
        for (TaskBO taskBO : lobbyTaskList) {
            redisTemplate.opsForHash().put(RedisConstantValue.TASK_LOBBY_LIBRARY, taskBO.getId(), taskBO);
        }
    }

    /**
     * 加载游戏任务库
     */
    public void loadGameTaskLibrary(){
        log.info("------ load game task library -----------");
        List<GameTaskBO> gameTaskList = taskGameMapper.getGameTaskList();
        log.info("------ gameTaskList : {}",gameTaskList );
        for (GameTaskBO gameTaskBO : gameTaskList) {
            Integer gameId = gameTaskBO.getGameId();
            for (TaskBO taskBO : gameTaskBO.getTaskList()) {
                redisTemplate.opsForHash().put(String.format(RedisConstantValue.TASK_GAME_LIBRARY,gameId), taskBO.getId(), taskBO);
            }
        }
    }
}
